package com.lxq.springBoot.jedis;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.lxq.springBoot.common.SerializeTool;

//@Component
public class HttpSessionWrapper implements HttpSession{

	@Autowired
	private JedisClient jedisClient;
	
	private String sid = "";
	
	private HttpServletRequest request;
	
	private HttpServletResponse response;
	
	public HttpSessionWrapper(String sid, HttpServletRequest request, HttpServletResponse response) {
		super();
		this.sid = sid;
		this.request = request;
		this.response = response;
	}

	private final long createionTime = System.currentTimeMillis();
	
	private final long lastAccessedTime = System.currentTimeMillis();

	private int expire_time = 60;
	
	
	
	@Override
	public Object getAttribute(String name) {
		String jsonStr = jedisClient.get(sid+":"+name);
		if (jsonStr != null || !StringUtils.isEmpty(jsonStr)) {
			jedisClient.extendExpire(sid+":"+name, expire_time);
		}
		return jsonStr;
	}

	@Override
	public Enumeration<String> getAttributeNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getCreationTime() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getLastAccessedTime() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getMaxInactiveInterval() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ServletContext getServletContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public HttpSessionContext getSessionContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object getValue(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String[] getValueNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void invalidate() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isNew() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void putValue(String arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeAttribute(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeValue(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setAttribute(String name, Object value) {
		if (value instanceof String) {
			jedisClient.set(sid+":"+name, (String)value);
		}else {
			jedisClient.set(sid+":"+name, SerializeTool.object2String(value));
		}
		jedisClient.extendExpire(sid+":"+name, expire_time);
	}

	@Override
	public void setMaxInactiveInterval(int arg0) {
		// TODO Auto-generated method stub
		
	}

}
